package com.js.data.common.utils;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

/**
 * RestTemplateUtil
 *
 * @作者 wujj
 * @描述 TODO
 * @创建时间 2023-05-16 18:45
 * @备注 无！
 **/
@Slf4j
public class RestTemplateUtil {

    private final static RestTemplate restTemplate = SpringUtil.getBean(RestTemplate.class);


    private static HttpHeaders headers = new HttpHeaders();

    static {
//        headers.set("auth-secretId", environment.getProperty("arcVideo.secretId"));
//        headers.set("auth-signVersion", environment.getProperty("arcVideo.signVersion"));
//        headers.set("auth-timestamp",environment.getProperty("arcVideo.timestamp"));
//        headers.set("auth-nonce", environment.getProperty("arcVideo.nonce"));
//        headers.set("auth-signature", environment.getProperty("arcVideo.signature"));
    }

    /**
     * 发送HttpGet请求
     *
     * @param url 请求路径
     * @return result
     */
    public static <T> T doGet(String url, Class<T> responseType) {
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<JSONObject> requestEntity = new HttpEntity<>(headers);
        return restTemplate.exchange(url, HttpMethod.GET, requestEntity, responseType).getBody();
    }


    /**
     * 发送HttpPost请求
     *
     * @param url 请求路径
     * @return result
     */
    public static <T> T doPost(String url, JSONObject body, Class<T> responseType) {
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<JSONObject> requestEntity = new HttpEntity<>(body, headers);
        return restTemplate.exchange(url, HttpMethod.POST, requestEntity, responseType).getBody();
    }
}
